import permission from './wa-permission/permission.js';
module.exports = {
	open_setting_permission() {
		permission.gotoAppPermissionSetting();
	},
	// 先判断权限 app 有则执行suc_fun否则执行fail_fun 弹窗提醒需要获取权限
	async check_permission(config) {
		let { k, suc_fun = false, fail_fun = false } = config;
		let { platform, permission_config } = this.COMMON_VAR;
		let permission_k = permission_config[k][platform]['system_key'];
		let _flag = null;
		switch (platform) {
			case 'ios':
				if (permission_k) {
					_flag = permission.judgeIosPermission(permission_k);
				} else {
					_flag = true;
				}
				// console.log(_flag, permission);
				break;
			case 'android':
				let MainActivity = plus.android.runtimeMainActivity();
				_flag = MainActivity.checkSelfPermission(permission_k) == -1 ? false : true;
				console.log(
					MainActivity.checkSelfPermission(permission_k),
					// MainActivity.shouldShowRequestPermissionRationale(permission_k),
					permission_k,
					'1.当前权限状态'
				);
				break;
		}
		if (suc_fun || fail_fun) {
			if (_flag) {
				// console.log(uni.getStorageSync(`${k}_reject`), 'before');
				uni.setStorageSync(`${k}_reject`, 0);
				// console.log(uni.getStorageSync(`${k}_reject`), 'after');
				suc_fun && suc_fun();
			} else {
				fail_fun && fail_fun();
			}
		} else {
			return _flag;
		}
	},
	// 点击弹窗的去设置后 ios直接跳转到自身应用设置，安卓则进行系统默认授权
	async authority_click_callback(system_key, com_key, callback = false) {
		let _this = this;
		let { platform } = this.COMMON_VAR;
		switch (platform) {
			case 'ios':
				_this.open_setting_permission();
				break;
			case 'android':
				let reject_count = uni.getStorageSync(`${com_key}_reject`) || 0;
				let _flag = false;
				// console.log(reject_count, com_key, '2.弹窗按钮点击确认');

				if (reject_count == 1) {
					// 拒绝过一次
					_this.open_setting_permission();
				} else {
					let _status = await permission.requestAndroidPermission(system_key);
					_flag = _status == 1 ? true : false;
					console.log(_status, 'requestAndroidPermission');
					if (_status == -1) {
						uni.setStorageSync(`${com_key}_reject`, 1);
					}
					callback && callback(_flag);
				}
				break;
		}
	},
	permission_to_check(k, callback = false) {
		let _this = this;
		_this.check_permission({
			k,
			suc_fun: function () {
				callback && callback();
			},
			fail_fun: function () {
				console.log('qweqwe');
				_this.popup_show(1);
			},
		});
	},
};
